﻿using Ecommerce.Domain.Dto;
using Ecommerce.Repository;
using Ecommerce.Repository.Impl.Leaves;
using Ecommerce.Repository.Impl.Use;
using Ecommerce.Repository.Interface.Eunms;
using Ecommerce.Repository.Interface.Leaves;
using Ecommerce.Repository.Interface.User;
using Ecommerce.Services.Eunm;
using Ecommerce.Services.Leaves;
using Ecommerce.Services.User;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;

namespace Ecommerce.API.Extensions
{
    public static class ServicesExtension
    {
        public static IServiceCollection Addcontext<T>(this IServiceCollection services,string? connectionstring,bool enablesqlLog) where T : DbContext
        {
            return services.AddDbContext<EFContext>(w =>
            {
                w.UseSqlServer(connectionstring, a =>
                {
                    a.EnableRetryOnFailure(
                        maxRetryCount:10,
                        maxRetryDelay:TimeSpan.FromSeconds(60),
                        errorNumbersToAdd:null
                        );
                }).EnableSensitiveDataLogging();
                if(enablesqlLog)
                {
                    w.UseLoggerFactory(LoggerFactory.Create(configure =>
                    {
                        configure.AddFilter((category,level)=>category==DbLoggerCategory.Database.Command.Name && level==LogLevel.Information).AddConsole();
                    }));
                }
            });
        }

        public static IServiceCollection AppServices(this IServiceCollection services)
        {
            services.AddScoped<IUseInfoServices,UserInfoServices>();
            services.AddScoped<IAccountRepository, AccopuntRepository>();
            services.AddScoped<ICityRepository, CityRepository>();
            services.AddScoped<IProvinceRepository, ProvinceRepository>();
            services.AddScoped<IUserInfoRepository, UserInfoRepository>();
            services.AddScoped<ICountyRepository, CountRepository>();
            services.AddScoped<IRoleRepository, RoleRepository>();


            services.AddScoped<IEunmServices, EunmServices>();
            services.AddScoped<IEnumRepository, EunmRepository>();
            services.AddScoped<ILeaveServices, LeaveServices>();
            services.AddScoped<ILeaveRepository, LeaveRepository>();
            services.AddScoped<IParmentRepository, PartmentRepository>();
            services.AddScoped<IRepository<LeacPart>, BaseRepository<LeacPart>>();
            return services;
        }
        /// <summary>
        /// 注册jwt
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="builder"></param>
        public static void AddJwt(this IServiceCollection collection,WebApplicationBuilder builder)
        {
            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(w =>
            {
                // 私钥
                var secretByte = Encoding.UTF8.GetBytes(builder.Configuration["Authentication:SecretKey"]);
                w.TokenValidationParameters = new TokenValidationParameters()
                {
                    // 验证发布者
                    ValidateIssuer = true,
                    // 发布者信息
                    ValidIssuer = builder.Configuration["Authentication:Issuer"],
                    // 验证接收者
                    ValidateAudience = true,
                    // 接收者
                    ValidAudience = builder.Configuration["Authentication:Audience"],
                    // 验证是否过期
                    ValidateLifetime = true,
                    // 验证私钥
                    IssuerSigningKey = new SymmetricSecurityKey(secretByte)
                };
            });

        }
    }
}
